package com.sky.service.User.ShoppingCart;

import com.sky.constant.BaseContext;
import com.sky.dto.shopping.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.SetMeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 褚金龙
 * @Date 2024/9/24 15:46
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService{
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 当前登录用户的id,在线程中创建的临时变量获得的
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
         // 首先先查询购物车是否有此商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果已经存在了，只需要将数量加1
         if(list!=null && list.size()>0){
             ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
         }else {
        //如果不存在，需要插入一条购物车数据
             Long dishId = shoppingCart.getDishId();
           Long setmealId = shoppingCart.getSetmealId();
             //判断传过来的是菜品还是套餐，因为前端要么传菜品id，要么传套餐id
             if(dishId!=null){
                 //如果传过来的是菜品
                 Dish dish = dishMapper.getById(dishId);
                 shoppingCart.setName(dish.getName());
                 shoppingCart.setImage(dish.getImage());
                 shoppingCart.setAmount(dish.getPrice());

             }

             else {
                 //本次传过来的是套餐
                 SetMeal setMeal = setMealMapper.getByIdSetMeal(setmealId);
                 shoppingCart.setName(setMeal.getName());
                 shoppingCart.setImage(setMeal.getImage());
                 shoppingCart.setAmount(setMeal.getPrice());

             }
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartMapper.insert(shoppingCart);

         }



    }


    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> queryShoppingCart() {
        //获取当前用户的id
        //根据用户id查询购物车
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list=  shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 修改购物车菜品数量
     * @param count
     */
    @Override
    public void updateCount(Integer count,Long id) {
        if(count>0){
            shoppingCartMapper.updateCount(count,id);
        }else {
            shoppingCartMapper.deleteShopCart(id);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteCart() {
        //删除
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper. deleteCart(currentId);
    }
}
